import { useEffect, useMemo, useState } from 'react';
import { useSelector, shallowEqual } from 'react-redux';
import { IReducerState, IRootReducerState } from '@/store/root-reducer';
import {
  Button,
  Popconfirm,
  Space,
  Table,
  Typography,
} from '@douyinfe/semi-ui';
import { IconDeleteStroked, IconEditStroked } from '@douyinfe/semi-icons';
import { ColumnProps } from '@douyinfe/semi-ui/lib/es/table';
import { PaginationProps } from '@douyinfe/semi-ui/lib/es/pagination';
import { usePageContext } from '../context';
import TablePagination, {
  onPageParamsChange,
} from '@/widgets/table-pagination';
import { pageSizeOpts } from '../helper';
import BaseColumns from './columns';
import { parentToTree } from '@/utils/converter';

const DepartmentTable = () => {
  const { Text } = Typography;

  const PageContext = usePageContext();
  const PageAction = useMemo(() => {
    return {
      queryParams: PageContext.queryParams,
      setQueryParams: PageContext.setQueryParams,
      addRecord: PageContext.addRecord,
      editRecord: PageContext.editRecord,
      delRecord: PageContext.delRecord,
      detailRecord: PageContext.detailRecord,
    };
  }, [PageContext]);

  const { department } = useSelector<IRootReducerState, any>(
    (state) => ({ department: state.department }),
    shallowEqual
  );
  const { entities, totalCount } = department;

  const treeitems = useMemo(
    () =>
      parentToTree(entities, Math.min(...entities.map((e: any) => e.parentId))),
    [entities]
  );

  const columns: ColumnProps[] = [
    {
      title: '部门名称',
      dataIndex: 'name',
      width: '30%',
      render: (text, record, index) => {
        return (
          <Text
            onClick={() => {
              PageAction.detailRecord(record[department.key]);
            }}
            link>
            {text}
          </Text>
        );
      },
    },
    ...BaseColumns,
    {
      title: '',
      dataIndex: 'action',
      render: (text, record, index) => {
        return (
          <Space>
            <Button
              icon={<IconEditStroked />}
              onClick={() => {
                PageAction.editRecord(record[department.key]);
              }}></Button>
            <Popconfirm title='attention' content='delete?'>
              <span className='inline-block invisible group-hover/item:visible'>
                <Button
                  icon={<IconDeleteStroked />}
                  type='danger'
                  onClick={() => {
                    PageAction.delRecord(record[department.key]);
                  }}></Button>
              </span>
            </Popconfirm>
          </Space>
        );
      },
      // // 此处从render的第四个形参中解构出 展开按钮、选择按钮、文本等内容
      // render: (text, record, index, { expandIcon, selection, indentText }) => {
      //   return <>111</>;
      // })
    },
  ];

  const onRowEvent: any = useMemo(
    () => (record: any, index: number) => {
      return {
        className: 'group/item',
      };
    },
    []
  );

  const paginationOptions = useMemo(
    () => ({
      total: totalCount,
      queryParams: PageAction.queryParams,
      pageSizeOpts,
    }),
    [PageAction, department]
  );

  return (
    <TablePagination paginationOpts={paginationOptions}>
      {(paginationOpts: PaginationProps) => (
        <Table
          size='small'
          columns={columns}
          dataSource={treeitems}
          onChange={(params: any) =>
            onPageParamsChange(params, PageAction.setQueryParams)
          }
          expandAllRows
          pagination={paginationOpts}
          onRow={onRowEvent}
        />
      )}
    </TablePagination>
  );
};

export default DepartmentTable;
